package com.solution.admin.coupon.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.coupon.entity.CouponGetRuleEntity;
import com.solution.admin.coupon.service.CouponGetRuleService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 优惠券领取、使用对象限制web控制层
 * @author lilp
 * @date 2018-07-10 16:28:05
 */
@RequestMapping("/couponGetRule")
@Controller
public class CouponGetRuleController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CouponGetRuleController.class);

	@Autowired
	private CouponGetRuleService couponGetRuleService;
	
	/**
	 * 跳转优惠券领取、使用对象限制查询页面
	 * @param CouponGetRule 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2018-07-10 16:28:05
	 */
	@RequestMapping("/toListCouponGetRulePage")
	public String toListCouponGetRulePage(CouponGetRuleEntity couponGetRule, HttpServletRequest request) {
		return "coupon/couponGetRule_list";
	}

	/**
	 * 分页查询优惠券领取、使用对象限制列表
	 * @param couponGetRule 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2018-07-10 16:28:05
	 */
	@RequestMapping("/listCouponGetRulePage")
	@ResponseBody
	public JSONGrid listCouponGetRulePage(CouponGetRuleEntity couponGetRule, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = couponGetRuleService.listCouponGetRulePage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询优惠券领取、使用对象限制失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2018-07-10 16:28:05
	 */
	@RequestMapping("/toAddCouponGetRule")
	public String toAddCouponGetRule(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "coupon/couponGetRule_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2018-07-10 16:28:05
	 */
	@RequestMapping("/toUpdateCouponGetRule")
	public String toUpdateCouponGetRule(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CouponGetRuleEntity couponGetRule = couponGetRuleService.getCouponGetRuleById(id);
			request.setAttribute("couponGetRule", couponGetRule);
		} catch (Exception e) {
			handleException("跳转编辑优惠券领取、使用对象限制页面错误", logger, e);
		}
		return "coupon/couponGetRule_form";
	}


	/**
	 * 新增或保存优惠券领取、使用对象限制
	 * @param request 请求对象
	 * @param couponGetRuleEntity 优惠券领取、使用对象限制实体
	 * @return
	 * @author lilp
 	 * @date 2018-07-10 16:28:05
	 */
	@RequestMapping("/saveOrUpdateCouponGetRule")
	@ResponseBody
	public Map<String, Object> saveCouponGetRule(HttpServletRequest request, CouponGetRuleEntity couponGetRuleEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = couponGetRuleService.updateCouponGetRule(couponGetRuleEntity);
			}else{
				result = (int)couponGetRuleService.saveCouponGetRule(couponGetRuleEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加优惠券领取、使用对象限制失败", logger, e);
		}
		return resultMap;
	}
	
}
